package yunquan.wang.jbpm5.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MainController extends HttpServlet {
 
	private static final long serialVersionUID = -2600475985219284847L;

	/**
	 * @param args
	 */
	@Override
	protected final void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

	    String method=param(req,"method");
	    if(method.equalsIgnoreCase("startProcess"))
	    {
	    	startProcess(req);
	    }
	 if(method.equalsIgnoreCase("sql"))
	 {
		 
	 }
	    
	    if(method.equalsIgnoreCase("login"))
	    {
	    	String userName=param(req,"userName");
	    	req.getSession().setAttribute("userName", userName);
	    }
	}
	
	private void listTask(HttpServletRequest req)
	{
    	String userName=req.getSession().getAttribute("userName").toString();
 
		JbpmTools.getClient().getTasksAssignedAsPotentialOwner(userName, JdbcUserGroupCallback.getuserGroups(userName), "en-UK");
	 
 
	}
	
	private void startProcess(HttpServletRequest req)
	{
		String process=param(req,"processId");
	 Map<String,Object> processParam=new HashMap<String,Object>();
	 //前端传入的参数，只取一个
	  Map<String,String[]> requestParam=req.getParameterMap();
	for(Entry<String, String[]>  single:requestParam.entrySet())  
	{
		processParam.put(single.getKey(), single.getValue()[0]);
	}
	//启动流程
    	JbpmTools.getSession().startProcess(process, processParam);
		
	}
	
	private String param(HttpServletRequest request,String param)
	{
		String value=request.getParameter(param);
		return value;
	}
}
